﻿using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;

namespace QWManage.Model
{
    public class AppConfigInfo
    {

        public bool YiYeMatched { set; get; } = false;
        #region 定时

        public bool Daily { set; get; } = false;
        public int Hour { set; get; } = 1;
        public int Minute { set; get; } = 0;

        public bool Groups { set; get; } = false;
        public int HourStart { set; get; } = 9;
        public int MinuteStart { set; get; } = 0;

        public int HourEnd { set; get; } = 21;
        public int MinuteEnd { set; get; } = 0;

        #endregion

        public string CorpName { set; get; } = "";
        /// <summary>
        /// 主体
        /// </summary>
        public string Corpid { set; get; } = "";
        /// <summary>
        /// 通讯录
        /// </summary>
        public string CorpsecretA { set; get; } = "";
        /// <summary>
        /// 客户联系
        /// </summary>
        public string CorpsecretB { set; get; } = "";

        public string DataSource { set; get; } = "";
        public string InitialCatalog { set; get; } = "";
        public string UserID { set; get; } = "";
        public string Password { set; get; } = "";
        public int ConnectTimeout { set; get; } = 30000;

        public string SqlConnStr
        {
            get
            {
                return $"" +
                    $"Data Source={DataSource};" +
                    $"Initial Catalog={InitialCatalog};" +
                    $"User ID={UserID};" +
                    $"Password={Password};" +
                    $"Connect Timeout={ConnectTimeout}";
            }
        }
    }
    public class AppCofingMgr
    {

        public static AppConfigInfo Get()
        {
            AppConfigInfo obj = null;
            try
            {
                string fn = Path.Combine(Application.StartupPath, "myappcfg.dll");
                if (File.Exists(fn))
                {
                    string json = File.ReadAllText(fn);
                    obj = JsonConvert.DeserializeObject<AppConfigInfo>(json);
                    return obj;
                }
            }
            catch { }
            if (obj == null) { obj = new AppConfigInfo(); }
            return obj;
        }

        public static void Set(AppConfigInfo info)
        {
            try
            {
                string fn = Path.Combine(Application.StartupPath, "myappcfg.dll");
                {
                    string json = JsonConvert.SerializeObject(info);
                    File.WriteAllText(fn, json);
                }
            }
            catch { }
        }
    }
}
